/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.domain.account.AccountFreezeLog;
import info.peigen.hotpot.business.account.core.repository.entity.AccountFreezeLogEntity;
import info.peigen.hotpot.business.account.manage.convertor.AccountFreezeLogManageConvertor;
import info.peigen.hotpot.business.account.manage.repository.mapper.AccountFreezeLogManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(AccountFreezeLogManageRepository)</b>
 * 管理后台 "账户冻结解冻记录" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-08-29
 */
@SuppressWarnings("unused")
@Component
public class AccountFreezeLogManageRepository extends AbstractBeetlSqlRepository<AccountFreezeLogManageMapper, AccountFreezeLogEntity> {

    public AccountFreezeLog active(String freezeType) {
        Assert.isTrue(StrUtil.isNotEmpty(freezeType), () -> new BusinessException(ResultCode.FAILURE, "激活时[freezeType]不得为空!"));

        AccountFreezeLogEntity entity = getMapper().findByFreezeType(freezeType);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!freezeType=" + freezeType));

        return AccountFreezeLogManageConvertor.convertToDomain(entity);
    }

    public AccountFreezeLog lock(String freezeType) {
        Assert.isTrue(StrUtil.isNotEmpty(freezeType), () -> new BusinessException(ResultCode.FAILURE, "加锁时[freezeType]不得为空!"));

        AccountFreezeLogEntity entity = getMapper().lockByFreezeType(freezeType);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!freezeType为空!"));

        return AccountFreezeLogManageConvertor.convertToDomain(entity);
    }

    public void removeByFreezeType(String freezeType) {
        Assert.isTrue(StrUtil.isNotEmpty(freezeType), () -> new BusinessException(ResultCode.FAILURE, "删除时[freezeType]不得为空!"));

        getMapper().deleteByFreezeType(freezeType);
    }

    public void store(AccountFreezeLog domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        AccountFreezeLogEntity entity = AccountFreezeLogManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}